class InternalDamageJudgeController < ApplicationController
  
  def part_selector
    get_order_item
    @order = get_order
    @order.use_amend_set = params[:frame]
    @order.save
    @direction_sides = @order.get_direction_side
    @positions_in_direction_side = @order.find_positions_in_dir(@direction_sides)
    @inside_positions_in_direction_side = InsidePosition.find_inside_position_in_dir(@direction_sides)
  end
  
  def judge_when_edit
    @order = get_order
    #得到session中存储的待编辑的order_item
    get_order_item
  end
  
  def check_position   
    
      selected_position = InsidePosition.find(params[:inside_position])
      positions = get_order.inside_positions
      position_exist = false
      positions.each{|position| 
      if position == selected_position
        position_exist = true  
      end}
      render :update do |page|
        if position_exist
          page.alert "#{selected_position.part}の部位がすでに診断されました。ほかの部位を選んでください"
        else
          page.replace_html "direction", selected_position.direction_side
          page.replace_html "position", selected_position.part
          page[:direction].visual_effect :highlight,
                                      :startcolor => "#88ff88",
                                      :endcolor => "#114411"
          page[:position].visual_effect :highlight,
                                      :startcolor => "#88ff88",
                                      :endcolor => "#114411"
          page.replace_html "work", params[:work]
          page[:work].visual_effect :highlight,
                                      :startcolor => "#88ff88",
                                      :endcolor => "#114411"
        end
        
      end
  end
  
  def check_position_edit
    render :update do |page|
      page.replace_html "work", params[:work]
      page[:work].visual_effect :highlight,
                                      :startcolor => "#88ff88",
                                      :endcolor => "#114411"
    end
  end
  
  #在改变选项内容时自动修改order中的use_amend_set属性并保存，然后高亮显示修正机信息栏的改变
  def recalculate_on_change
    order = get_order
    use_amend_set = params[:frame]
    order.use_amend_set = use_amend_set
    order.change_order_items_value(order.use_amend_set)
    
    #计算合计值
    order.count_sum_index  
    #判断是否要高亮显示合计栏的变化，只有当有内板部位存在时才有必要变化合计栏
    change_total_time_column = false
    render :update do |page|
      if order.use_amend_set
        page.replace_html "use_amend_set", "使用"
      else
        page.replace_html "use_amend_set", "使用しない"
      end
    page[:use_amend_set].visual_effect :highlight,
                                      :startcolor => "#88ff88",
                                      :endcolor => "#114411"   
    page.replace_html("order_item",:partial => "admin/order_items",:locals => {:order_items => order.order_items,
                                                         :order => order,
                                                         :time_index_of_uninstall => order.time_index_of_uninstall,
                                                         :time_index_of_putty => order.time_index_of_putty,
                                                         :time_index_of_surface => order.time_index_of_surface, 
                                                         :time_index_of_paint => order.time_index_of_paint})
    order.order_items.each do |order_item|
      if order_item.position_id == nil
        change_total_time_column = true
        page["order_item_#{order_item.id}"].visual_effect :highlight,
                                                          :startcolor => "#88ff88",
                                                          :endcolor => "#114411"
      end
    end
    #判断是否变化合计栏
    if change_total_time_column
       page[:total_time].visual_effect :highlight,
                                    :startcolor => "#88ff88",
                                    :endcolor => "#114411"
    end

    end
    
    
  end
    
    def save_inside_position
      order = get_order
      work_type = params[:work]
      inside_position = params[:inside_position]
      
      order_item = get_order_item
      #把选中的内板部位赋给order_item相应的属性
      inside_position = InsidePosition.find(inside_position)
      order_item.inside_position = inside_position
      
      calculate_for_inside_position(order,work_type,order_item)
    end
    
    def save_when_edit
      #取得order
      order = get_order
      work_type = params[:work]
      order_item = get_order_item
      calculate_for_inside_position(order,work_type,order_item)
    end
    
    private
    
    #计算式
    def calculate_for_inside_position(order,work_type,order_item)
      
      inside_position = order_item.inside_position
      #取得order对象的set使用信息（true/false），并根据其情况来计算结果
      use_frame_modifier = order.use_amend_set
      if use_frame_modifier
        order_item.metal_index = 0
        order_item.cut_index = 0
        if work_type == "修正"
          order_item.work_judge = "修正機set_修正から"
          order_item.frame_amend_index = inside_position.metal_index
          order_item.frame_amend_set_index = 1.0
        elsif work_type == "取替"  
          order_item.work_judge = "修正機set_カットから"
          order_item.frame_amend_index = inside_position.exchange_index
          order_item.frame_amend_set_index = 1.0
        end
      else
        order_item.frame_amend_index = 0
        order_item.frame_amend_set_index = 0
        if work_type == "修正"
          order_item.work_judge = "修正から"
          order_item.metal_index = inside_position.metal_index
          order_item.cut_index = 0
        elsif work_type == "取替" 
          order_item.work_judge = "カットから"
          order_item.cut_index = inside_position.exchange_index
          order_item.metal_index = 0
        end
      end
      order.order_items << order_item
      if order.save 
        redirect_to :controller => :admin, :action => :show_info
      end
    end
  end
